package mysql;

import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import model.ArticleModel;
public class MysqlAction {
	private Connection connection = null;
	public MysqlAction(){
		connection = CreateConnection.getConnection();
		
	}
	
	public ArticleModel getSingleArticle(int id){
		ArticleModel articleModel = null;
		String sql = "select * from article where article_id=?";
		try{
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			
			ResultSet resultSet = preparedStatement.executeQuery();
			
			while(resultSet.next()){
				articleModel = new ArticleModel();
				articleModel.setArticle_id(resultSet.getInt("article_id"));
				articleModel.setArticle_time(resultSet.getString("article_time"));
				articleModel.setArticle_title(resultSet.getString("article_title"));
				articleModel.setArticle_content(resultSet.getString("article_content"));
				articleModel.setAritcle_author(resultSet.getString("article_author"));
				articleModel.setArticle_click(resultSet.getInt("article_click"));
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return articleModel;
	}
	
	public List<ArticleModel> getAllArticle(){
		List<ArticleModel> articles = new ArrayList<ArticleModel>();
		
		String sql = "select * from article";
		
		try{
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			ResultSet resultSet = preparedStatement.executeQuery();
			while(resultSet.next()){
				ArticleModel articleModel = new ArticleModel();
				articleModel.setArticle_id(resultSet.getInt("article_id"));
				articleModel.setArticle_time(resultSet.getString("article_time"));
				articleModel.setArticle_title(resultSet.getString("article_title"));
				articleModel.setArticle_content(resultSet.getString("article_content"));
				articleModel.setAritcle_author(resultSet.getString("article_author"));
				articleModel.setArticle_click(resultSet.getInt("article_click"));
				articles.add(articleModel);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return articles;
	}
}
